package com.volunteer.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.volunteer.common.R;
import com.volunteer.entity.Notice;
import com.volunteer.service.NoticeService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/notice")
@AllArgsConstructor
public class AdminNoticeController {
    private final NoticeService noticeService;

    // 获取所有公告
    @GetMapping("/list")
    public R<List<Notice>> list() {
        List<Notice> list = noticeService.list(new LambdaQueryWrapper<Notice>().orderByDesc(Notice::getCreateTime));
        return R.ok(list);
    }

    // 新增公告
    @PostMapping("/add")
    public R<String> add(@RequestBody Notice notice) {
        boolean success = noticeService.save(notice);
        return success ? R.ok("新增成功") : R.fail("新增失败");
    }

    // 更新公告
    @PostMapping("/update")
    public R<String> update(@RequestBody Notice notice) {
        boolean success = noticeService.updateById(notice);
        return success ? R.ok("更新成功") : R.fail("更新失败");
    }

    // 删除公告
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {
        boolean success = noticeService.removeById(id);
        return success ? R.ok("删除成功") : R.fail("删除失败");
    }
}
